/**
 * Created by yangh on 2016/09/14.
 */

var main = {};
var myScroll;
var canLoad=true;
(function ($) {
	main.init = function () {
	
		document.addEventListener('touchmove', function (e) {
			e.preventDefault();
		}, false);
	
	};

	/**
	 * @func move()
	 * @desc 监听滚动条的位置，异步加载列表
	 */
	function move() {
		if(!canLoad){
    		return;
    	}
		if (this.y < main.max - 80) {
			canLoad=false;
			$("#tip").hide();
			$("#loading").show();

			//加载数据
			main.loadData(".evaluationList");
			
			this.refresh();
			main.max = this.maxScrollY;
			console.log(main.max);

			$("#loading").hide();
			$("#tip").show();
		}
	}

	/**
	 * @func main.scroll()
	 * @desc 配置iscroll插件
	 */
	main.scroll = function () {
		myScroll = new IScroll("#wrapper", {probeType: 3, mouseWheel: true, click: true, preventDefault: false});

		main.max = myScroll.maxScrollY;
		/*console.log(main.max);*/

		myScroll.on("scroll", move);
	}
	
	
	
	 /** 
     * function 
     * loadData
     * @param target  elements append to target 
     */
    main.loadData = function(target){
    	//ajax begin
    	 var merchantid=$("#merchantid").val();
    	 var goodsid=$("#goodsid").val();
    	 var index = -1;
    	 if($(target+" li").length!=0){
    		 index=$(target+" li:last").data("id");
    	 }
         var html = "";
         $.ajax({
     		type:"post",
     		async:true, 
        	url:path+"/comment/more",
        	data:{
        		goodsid:goodsid,
        		merchantid:merchantid,
        		index:index
        	},
     		success:function(data){	
     			if('000' == data.code){
     				
     				var html = "";
     				for (var i = 0; i < data.result.length; i++) {
     					var comment = data.result[i];
     					html +="<li class=\"evaluation\"  data-id='"+comment.id+"'>";
     					html +="<div class=\"userInfo\">";
 						html +="<div class=\"portrait\">";
 						html +="<img src='"+comment.member.headimgurl+"' alt=\"\">";
 						html +="</div>";
 						html +="<span class=\"username\">"+comment.member.id+"</span>";
 						html +="<br>";
 						html +="<span class=\"scoreCon\" data-score="+comment.score+"></span>";
 						html +="<span class=\"date\">"+comment.createTime+"</span>";
 						html +="</div>";
 						html +="<div class=\"detail\">";
 						html +="<p class=\"content\">"+comment.content+"</p>";
 						html +="<div class=\"pics\">";
 						for(var j=0; j<comment.picList.length ;j++){
     						var item = comment.picList[j];
     						html +="<div>";
     						html +="<img src='"+item.picurl+"' alt=\"\">";
     						html +="</div>";
 						}
 						html +="</div>";
 						html +="</div>";
						html +="</li>";
     				}	
     				//加载
     				$(target).append(html);
     				if(data.result.length>=10){
  		            	$("#loading").hide();
		  	            $("#tip").show();
		  	            canLoad=true;
  		            }else{
  		            	$("#loading").hide();
  		            	$("#tip").hide();
  		            	/*$("#tip").text("我是有底线的");*/
  		            }
     				 /* 评分 */
     		        score($(".scoreCon"));
     		        
     				myScroll.refresh();
     	            main.max = myScroll.maxScrollY;
     			}
     		}
     	})
   		//ajax end
     	
     
    }
})(jQuery);

main.init();
//加载数据
main.loadData(".evaluationList");

